$(function () {
    //声明变量
    var rechargeTime_form = $("#rechargeTime_form");
    var rechargeTime_datagrid =   $("#rechargeTime_datagrid");
    var rechargeTimeMin_datagrid =   $("#rechargeTimeMin_datagrid");

    //右侧会员信息表格
    rechargeTimeMin_datagrid.datagrid({
        url:'/member/query.do',
        fitColumns:true,
        title:"会员信息表",
        width:850,
        height:312,
        rownumber:true,
        singleSelect:true,
        columns:[[
            {field:'id',checkbox:true},
            {field:'cardNumber',title:'会员卡号',width:80},
            {field:'name',title:'会员姓名',width:80},
            {field:'levelName',title:'会员等级',width:80},
            {field:'birthday',title:'会员生日',width:80},
            {field:'address',title:'地址',width:80},
            {field:'groupName',title:'分组类型',width:80},
            {field:'points',title:'积分',width:80}
        ]]
    });

    //下面充次表格
        rechargeTime_datagrid.datagrid({
        url:'/rechargeTime/query.do',
        width:1350,
        height:250,
        title:"会员充次信息表",
        fitColumns:true,
        pagination:true,
        rownumber:true,
        singleSelect:true,
        columns:[[
            {field:'id',checkbox:true},
            {field:'cardNumber',title:'卡号',width:80},
            {field:'productName',title:'服务名称',width:80},
            {field:'beforeTime',title:'充值前次数',width:80},
            {field:'time',title:'充值次数',width:80},
            {field:'afterTime',title:'充值后次数',width:80},
            {field:'operation',title:'支付方式',width:80},
            {field:'recharge',title:'支付金额',width:80},
            {field:'rechargeDate',title:'充值时间',width:80},
            {field:'validDate',title:'有效时间',width:80},
            {field:'remark',title:'备注',width:80}
        ]],

        //点击一行数据的时候触发
        onClickRow:function (index,row) {
            if(row){
                var beforeTime=row.beforeTime;
                var rows=rechargeTime_datagrid.datagrid("getSelected");
                //发送sql去查询会员的所有信息
                $.get("/member/selectByRechargeTimeId.do",{id:rows.id},function (data) {
                    //再把对应的值填充到表单中去
                    $("#cardNumber").textbox("setValue",data.cardNumber);
                    $("#name").textbox("setValue",data.name);
                    $("#levelName").textbox("setValue",data.levelName);
                    $("#birthday").textbox("setValue",data.birthday);
                    $("#point").textbox("setValue",data.points);
                    $("#total").textbox("setValue",data.total);
                    $("#amount").textbox("setValue",data.amount);
                    $("#idd").val(rows.id);
                    $("#beforeTime").val(beforeTime);
                });
            }
        }
    });

    //充次的时候, 点击充值, 页面跳转到充值页面
    $("#recharge").click(function () {
        window.location.href="/memberClient/view.do";
    });

    //统一函数
    var methodObj = {

        //充次的时候, 提交表单
        rechargeTime:function (){
        //判断是否选择了一条数据
        var row =rechargeTime_datagrid.datagrid("getSelected");
        if(row){
            $.messager.confirm("确认对话框","确认要充次吗?",function (r) {
                if(r){
                    //提交表单
                    rechargeTime_form.form("submit",{
                        url:'/rechargeTime/saveOrUpdate.do',
                        success:function (data) {
                            //回调函数:data表示返回数据解析为json
                            data = $.parseJSON(data);
                            if (data.success) {
                                //如果返回结果成功__弹出窗口提示
                                $.messager.alert('温馨提示','充次成功','info',function () {
                                    rechargeTime_datagrid.datagrid('reload');
                                    $("#rechargeTime_form").form("clear");
                                })

                            }else {
                                //如果保存失败,则弹出窗口提示
                                $.messager.alert('温馨提示',data.msg,'error');
                            }
                        }
                    });
                }
            });
        }else{
            $.messager.alert('温馨提示',"请选择一条数据",'error');
            return;
        }
        },

        query:function (){
            //获取查询条件
            var keyword =  $("#keyword").textbox('getValue');
            //通过datagrid 的 load方法传递参数
            rechargeTime_datagrid.datagrid('load',{
                keyword:keyword
            });
        }};

    //统一绑定事件:
    $("a[data-cmd]").click(function () {
        var methodname = $(this).data("cmd");
        //动态获取调用方法名字,调用方法;
        methodObj[methodname]();
    });

    //合计充值金额
    $("input[name='recharge']").blur(function () {
        var td=$(this).closest("td");
        var recharge=td.find("[name='recharge']").val() || 0;
        var recharge2=parseFloat(recharge);
        //把和放入充值列中
        $("#totalAmount").html(recharge2);
    });

});

